// @before-stub-for-debug-begin
#include <vector>
#include <string>
#include <queue>
#include "commoncppproblem515.h"

using namespace std;
// @before-stub-for-debug-end

/*
 * @lc app=leetcode.cn id=515 lang=cpp
 *
 * [515] 在每个树行中找最大值
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<int> largestValues(TreeNode* root) {
        vector<int> res;
        if(root == NULL)
            return res;
        TreeNode* point;
        queue<TreeNode*> q;
        q.push(root);
        while (!q.empty())
        {
            int size = q.size();
            int max = INT_MIN;
            for(int i = 0;i<size;i++)
            {
                point = q.front();
                q.pop();
                if(point->val > max)
                    max = point->val;
                if(point->left)
                    q.push(point->left);
                if(point->right)
                    q.push(point->right); 
            }
            res.push_back(max);
        }
        return res;
    }
};
// @lc code=end

